package com.slz3.business.module.financeManagement.finFinanceManage.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.financeManagement.finFinanceManage.domain.FinFinanceManage;
import com.slz3.business.module.financeManagement.finFinanceManage.domain.FinFinanceManageFactory;
import com.slz3.business.module.financeManagement.finFinanceManage.steward.FinFinanceManageSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class FinFinanceManageServiceImpl implements FinFinanceManageService {
    @Override
    public FinFinanceManage save(FinFinanceManage finFinanceManage) {
        return Repositorys.finFinanceManage.save(FinFinanceManageFactory.initBaseFields(finFinanceManage));
    }

    @Override
    public Page<FinFinanceManage> page(MultiValueMap<String, String> params) {
        Page<FinFinanceManage> finFinanceManagePage = Repositorys.finFinanceManage.findAll(
                FinFinanceManageSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return finFinanceManagePage;
    }


    @Override
    public FinFinanceManage modify(FinFinanceManage finFinanceManageNew) {
        return Repositorys.finFinanceManage.saveAndFlush(FinFinanceManageFactory.initBaseFields(finFinanceManageNew));
    }

    @Override
    public Optional<FinFinanceManage> single(UUID uuid) {
        return Repositorys.finFinanceManage.findById(uuid);
    }

    @Override
    public List<FinFinanceManage> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.finFinanceManage.findAllById(uuids);
    }

    @Override
    public List<FinFinanceManage> delete(List<FinFinanceManage> finFinanceManageList) {
        finFinanceManageList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            FinFinanceManageFactory.initBaseFields(item);
        });
        return Services.finFinanceManageTransactional.batchModify(finFinanceManageList);
    }

}
